package ${package}.${moduleName}.controller.admin;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ${comments}
 *
 * @author ${author}
 * @date ${datetime}
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys${pathName}" )
@Api(value = "${comments}(后台)", tags = "${comments}-后台接口")
@ApiSort(99)
public class ${className}SysApi {

    private final ${className}Service ${classname}Service;

    /**
    * 分页查询
    * @param currentPage 当前页数
    * @param pageSize 每页数据量
    * @param ${classname} 查询条件
    * @return 查询结果
    */
    @GetMapping("/page")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页查询", notes = "传入分页信息与查询条件")
    @PreAuthorize("@el.check('super','${pathName}_page')")
    public OutputObj<Page<${className}>> get${className}Page(
            @ApiParam(value = "当前页数", required = true, defaultValue = "1") @RequestParam Long currentPage,
            @ApiParam(value = "每页数据量", required = true, defaultValue = "10") @RequestParam Long pageSize,
            @ApiParam(value = "查询条件") ${className} ${classname}) {
        Page<${className}> page = new Page<${className}>(currentPage, pageSize);
        return OutputObj.ok(${classname}Service.page(page, Wrappers.query(${classname}).lambda().orderByDesc(${className}::getCreateTime)), "分页信息查询成功");
    }


    /**
    * 通过id查询${comments}
    * @param id id
    * @return Result
    */
    @GetMapping("/getById")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "通过id查询${comments}", notes = "传入id")
    @PreAuthorize("@el.check('super','${pathName}_getById')")
    public OutputObj getById(@ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        return OutputObj.success(${classname}Service.getById(id));
    }

    /**
    * 新增${comments}
    * @param ${classname} ${comments}
    * @return Result
    */
    @PostMapping("/add")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增${comments}", notes = "传入${className}数据")
    @PreAuthorize("@el.check('super','${pathName}_add')")
    public OutputObj save(@RequestBody @Valid ${className} ${classname}) {
        Assert.isTrue(${classname}Service.save(${classname}), "操作失败");
        return OutputObj.success("新增成功");
    }

    /**
    * 修改${comments}
    * @param ${classname} ${comments}
    * @return Result
    */
    @PostMapping("/edit")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改${comments}", notes = "传入${className}数据")
    @PreAuthorize("@el.check('super','${pathName}_edit')")
    public OutputObj updateById(@RequestBody @Valid ${className} ${classname}) {
        return OutputObj.success(${classname}Service.updateById(${classname}), "修改成功");
    }

    /**
    * 通过id删除${comments}
    * @param ids id集合
    * @return Result
    */
    @GetMapping("/del")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "通过id删除${comments}", notes = "传入id，多个id用英文逗号分隔")
    @PreAuthorize("@el.check('super','${pathName}_del')")
    public OutputObj<Boolean> removeById(
            @ApiParam(value = "id，多个id用英文逗号分隔", required = true) @RequestParam("ids") String ids) {
        String[] split = ids.split(",");
        if (split.length < 1) {
            return OutputObj.fail(ResultEnum.CRUD_PARAM_NOT.getDesc());
        }
        List<Long> idList = new ArrayList<Long>();
        Arrays.stream(split).forEach(s -> {
            if (StrUtil.isNotBlank(s)) {
                idList.add(Long.parseLong(s));
            }
        });
        Assert.isTrue(${classname}Service.removeByIds(idList), "操作失败");
        return OutputObj.success("删除成功");
    }
}